package com.itheima.reggin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggin.common.R;
import com.itheima.reggin.dto.SetmealDto;
import com.itheima.reggin.entity.Category;
import com.itheima.reggin.entity.Setmeal;
import com.itheima.reggin.service.CategoryService;
import com.itheima.reggin.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 */

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 增
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }

    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        Page<SetmealDto> dtoPage = new Page<>();
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null,Setmeal::getName,name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo,queryWrapper);
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> list = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if(category != null){
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */

    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }

    /**
     * 根据id 查询套餐信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> selectSetMeal(@PathVariable Long id){

        SetmealDto setmealDto = setmealService.selectWithDishs(id);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐保存
     * @param setmealDto
     * @return
     */
    @CacheEvict(value = "setmealCache",key = "'setmeal_'+#setmealDto.getCategoryId+'_'+#setmealDto.getStatus()")
    @PutMapping
    public R<String> saveUpdataSetMeal(@RequestBody SetmealDto setmealDto){

        setmealService.saveWithDishById(setmealDto);
        return R.success("修改套餐成功");
    }


    /**
     * 套餐停售/起售----批量
     */
    @PostMapping("/status/{ss}")
    public R<String> updataStatus(@PathVariable Integer ss,@RequestParam  List<Long> ids){
        log.info("进入套餐起售停售方法！:"+ss+"----"+ids);

        for (int i = 0; i < ids.size(); i++) {
            Setmeal setmeal = setmealService.getById(ids.get(i));
            setmeal.setStatus(ss);
            setmealService.updateById(setmeal);
        }
        return R.success("操作成功！");

    }

    /**根据条件查询数据
     * 查询套餐里面有那些菜
     * @param setmeal
     * @return
     */
    @Cacheable(value = "setmealCache",key = "'setmeal_'+#setmeal.getCategoryId+'_'+#setmeal.getStatus()")
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        //条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(setmeal.getName()), Setmeal::getName, setmeal.getName());
        queryWrapper.eq(null != setmeal.getCategoryId(), Setmeal::getCategoryId, setmeal.getCategoryId());
        queryWrapper.eq(null != setmeal.getStatus(), Setmeal::getStatus, setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        return R.success(setmealService.list(queryWrapper));
    }
}
